const LogModel = require('../models/logModel')

class LogController {
    static async findList (ctx) {
        let { page, size } = ctx.query
        page = Number(page)
        size = Number(size)

        const list = await LogModel.find()
                                   .sort({ _id: -1 })
                                   .skip((page - 1) * size)
                                   .limit(size)
                                   .exec()
        const total = await LogModel.countDocuments().exec()
        ctx.body = {
            code: 1,
            msg: '获取日志列表成功',
            data: {
                list,
                total,
            },
        }
    }

    static async deleteAll (ctx) {
        const res = await LogModel.deleteMany({}) //删除所有文档
        if (!res) {
            ctx.body = {
                code: 0,
                msg: '删除失败',
            }
            return
        }
        ctx.body = {
            code: 1,
            msg: '数据已全部清空',
            data: res,
        }
    }

    static async remove (ctx) {
        const { id } = ctx.params
        const res = await LogModel.deleteOne({
            _id: id,
        })
        ctx.body = {
            code: 1,
            data: res,
            msg: '删除成功',
        }
    }

    static async find (ctx) {}
}

module.exports = LogController
